import _Lov from "choerodon-ui/pro/lib/lov";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isNumber from "lodash/isNumber";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _class, _class2, _class3;

import React, { PureComponent } from 'react';
import { Modal, Form, Input, Select, InputNumber, Table } from 'antd';
import { Bind } from 'lodash-decorators';
import Switch from 'components/Switch';
import Lov from 'components/Lov';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import { tableScrollWidth } from 'utils/utils';
import styles from "./index.less?modules";
/**
 * Form.Item 组件label、wrapper长度比例划分
 */

var FormItem = Form.Item;
var Option = Select.Option;
var formLayout = {
  labelCol: {
    span: 6
  },
  wrapperCol: {
    span: 16
  }
};
/**
 * 发送配置-数据修改滑窗(抽屉)
 * @extends {PureComponent} - React.PureComponent
 * @reactProps {string} anchor - 抽屉滑动位置
 * @reactProps {string} title - 抽屉标题
 * @reactProps {boolean} visible - 抽屉是否可见
 * @reactProps {Function} onOk - 抽屉确定操作
 * @reactProps {Object} form - 表单对象
 * @reactProps {Object} itemData - 操作对象
 * @return React.element
 */

var Drawer = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec(_class = (_class2 = (_class3 = /*#__PURE__*/function (_PureComponent) {
  _inherits(Drawer, _PureComponent);

  var _super = _createSuper(Drawer);

  function Drawer() {
    var _this;

    _classCallCheck(this, Drawer);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      selectedRowKeys: [],
      selectedRows: []
    };
    return _this;
  }

  _createClass(Drawer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var onRef = this.props.onRef;
      onRef(this.handleClearSelection);
    }
  }, {
    key: "handleTableChange",
    value: function handleTableChange() {
      var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          _ref$current = _ref.current,
          current = _ref$current === void 0 ? 1 : _ref$current,
          _ref$pageSize = _ref.pageSize,
          pageSize = _ref$pageSize === void 0 ? 10 : _ref$pageSize;

      var onFetch = this.props.onFetch;
      onFetch({
        page: current - 1,
        size: pageSize
      });
    }
    /**
     * 确定操作
     */

  }, {
    key: "saveBtn",
    value: function saveBtn() {
      var _this$props = this.props,
          form = _this$props.form,
          onOk = _this$props.onOk,
          onEditOk = _this$props.onEditOk,
          itemData = _this$props.itemData;
      var tempServerLineId = itemData.tempServerLineId;

      if (onOk) {
        form.validateFields(function (err, values) {
          if (_isEmpty(err)) {
            // 校验通过，进行保存操作
            if (_isEmpty(itemData)) {
              onOk(values);
            } else {
              onEditOk(_objectSpread(_objectSpread({}, values), {}, {
                tempServerLineId: tempServerLineId
              }));
            }
          }
        });
      }
    }
    /**
     * 改变消息类型，重置服务代码
     *
     * @memberof DetailModal
     */

  }, {
    key: "changeTypeCode",
    value: function changeTypeCode() {
      var setFieldsValue = this.props.form.setFieldsValue;
      setFieldsValue({
        serverId: undefined,
        serverName: undefined
      });
    }
  }, {
    key: "getServerName",
    value: function getServerName(value, record) {
      var setFieldsValue = this.props.form.setFieldsValue;
      setFieldsValue({
        serverName: record.serverName,
        serverCode: record.serverCode
      });
    }
  }, {
    key: "onChangeTemplate",
    value: function onChangeTemplate(value, record) {
      var _this$props$form = this.props.form,
          setFieldsValue = _this$props$form.setFieldsValue,
          registerField = _this$props$form.registerField;
      registerField('templateName');
      setFieldsValue({
        templateName: record.templateName
      });
    }
  }, {
    key: "handleRowSelectionChange",
    value: function handleRowSelectionChange(_, selectedRows) {
      this.setState({
        selectedRowKeys: selectedRows.map(function (r) {
          return r.tempServerWhId;
        }),
        selectedRows: selectedRows
      });
    }
  }, {
    key: "handleClearSelection",
    value: function handleClearSelection() {
      this.setState({
        selectedRowKeys: [],
        selectedRows: []
      });
    }
    /**
     * 取消保存
     */

  }, {
    key: "handleDeleteWebHook",
    value: function handleDeleteWebHook() {
      var _this2 = this;

      var _this$state$selectedR = this.state.selectedRows,
          selectedRows = _this$state$selectedR === void 0 ? [] : _this$state$selectedR;
      Modal.confirm({
        title: intl.get("cpaas.common.message.confirm.title").d('提示'),
        content: intl.get("cmsg.sendConfig.view.message.title.content").d('确定删除吗？'),
        onOk: function onOk() {
          var _this2$props$onDelete = _this2.props.onDelete,
              onDelete = _this2$props$onDelete === void 0 ? function (e) {
            return e;
          } : _this2$props$onDelete;
          onDelete(selectedRows);
        }
      });
    }
    /**
     * render
     * @returns React.element
     */

  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          anchor = _this$props2.anchor,
          visible = _this$props2.visible,
          title = _this$props2.title,
          form = _this$props2.form,
          itemData = _this$props2.itemData,
          onCancel = _this$props2.onCancel,
          _this$props2$messageT = _this$props2.messageType,
          messageType = _this$props2$messageT === void 0 ? [] : _this$props2$messageT,
          tenantId = _this$props2.tenantId,
          fetchLoading = _this$props2.fetchLoading,
          deleteLoading = _this$props2.deleteLoading,
          _this$props2$webHook = _this$props2.webHook,
          _this$props2$webHook$ = _this$props2$webHook.pagination,
          pagination = _this$props2$webHook$ === void 0 ? {} : _this$props2$webHook$,
          _this$props2$webHook$2 = _this$props2$webHook.webHookData,
          webHookData = _this$props2$webHook$2 === void 0 ? [] : _this$props2$webHook$2,
          ds = _this$props2.ds,
          path = _this$props2.path;
      var getFieldDecorator = form.getFieldDecorator,
          getFieldValue = form.getFieldValue;
      var isEdit = !_isEmpty(itemData);
      var selectedRowKeys = this.state.selectedRowKeys;
      var columns = [{
        title: intl.get('cmsg.common.view.accountCode').d('账户代码'),
        dataIndex: 'serverCode',
        width: 100
      }, {
        title: intl.get('cmsg.common.view.accountName').d('账户名称'),
        dataIndex: 'serverName'
      }, {
        title: intl.get('cmsg.sendConfig.model.sendConfig.accountType').d('账户类型'),
        dataIndex: 'serverTypeMeaning',
        width: 100
      }];
      return /*#__PURE__*/React.createElement(Modal, {
        title: title,
        width: 520,
        wrapClassName: "ant-modal-sidebar-".concat(anchor),
        transitionName: "move-".concat(anchor),
        visible: visible,
        onOk: this.saveBtn,
        okText: intl.get('cpaas.common.button.sure').d('确定'),
        onCancel: onCancel,
        cancelText: intl.get('cpaas.common.button.cancel').d('取消'),
        zIndex: 999,
        destroyOnClose: true
      }, /*#__PURE__*/React.createElement(Form, null, /*#__PURE__*/React.createElement(Form.Item, Object.assign({
        label: intl.get('cmsg.sendConfig.model.sendConfig.templateId').d('模板代码')
      }, formLayout), getFieldDecorator('templateCode', {
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.sendConfig.model.sendConfig.templateCode').d('模板代码')
          })
        }],
        initialValue: itemData.templateCode
      })( /*#__PURE__*/React.createElement(Lov, {
        code: "CMSG.TEMP_SERVER.MESSAGE_TEMP",
        textValue: itemData.templateCode,
        disabled: !_isNumber(tenantId),
        queryParams: {
          tenantId: tenantId
        },
        onChange: this.onChangeTemplate
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('cmsg.sendConfig.model.sendConfig.typeCode').d('消息类型')
      }, formLayout), getFieldDecorator('typeCode', {
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.sendConfig.model.sendConfig.typeCode').d('消息类型')
          })
        }],
        initialValue: itemData.typeCode
      })( /*#__PURE__*/React.createElement(Select, {
        allowClear: true,
        disabled: isEdit,
        onChange: this.changeTypeCode
      }, messageType && messageType.map(function (item) {
        return /*#__PURE__*/React.createElement(Option, {
          value: item.value,
          key: item.value
        }, item.meaning);
      })))), itemData.typeCode !== 'WEB_HOOK' && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Form.Item, Object.assign({
        label: intl.get('cmsg.common.view.accountCode').d('账户代码')
      }, formLayout), getFieldDecorator('serverCode', {
        rules: [{
          required: getFieldValue('typeCode') !== 'WEB' && getFieldValue('typeCode') !== 'WEB_HOOK',
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.common.view.accountCode').d('账户代码')
          })
        }],
        initialValue: itemData.serverCode
      })( /*#__PURE__*/React.createElement(Lov, {
        code: "CMSG.SERVER",
        textValue: itemData.serverCode,
        disabled: !getFieldValue('typeCode') || !_isNumber(tenantId) || getFieldValue('typeCode') === 'WEB' || getFieldValue('typeCode') === 'WEB_HOOK',
        queryParams: {
          typeCode: getFieldValue('typeCode'),
          tenantId: tenantId
        },
        onChange: this.getServerName
      }))), /*#__PURE__*/React.createElement(Form.Item, Object.assign({
        label: intl.get('cmsg.common.view.accountName').d('账户名称')
      }, formLayout), getFieldDecorator('serverName', {
        rules: [{
          required: false,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.common.view.accountName').d('账户名称')
          })
        }],
        initialValue: itemData.serverName
      })( /*#__PURE__*/React.createElement(Input, {
        disabled: true
      })))), /*#__PURE__*/React.createElement(Form.Item, Object.assign({}, formLayout, {
        label: intl.get('cmsg.sendConfig.model.sendConfig.tryTimes').d('重试次数')
      }), getFieldDecorator('tryTimes', {
        initialValue: itemData.tryTimes
      })( /*#__PURE__*/React.createElement(InputNumber, {
        step: 1,
        min: 0,
        disabled: form.getFieldValue('typeCode') !== 'EMAIL'
      }))), /*#__PURE__*/React.createElement(Form.Item, Object.assign({
        label: intl.get('cpaas.common.remark').d('备注')
      }, formLayout), getFieldDecorator('remark', {
        rules: [{
          required: false,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cpaas.common.remark').d('备注')
          })
        }],
        initialValue: itemData.remark
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(Form.Item, Object.assign({
        label: intl.get('cpaas.common.status').d('状态')
      }, formLayout), getFieldDecorator('enabledFlag', {
        initialValue: itemData.enabledFlag !== undefined ? itemData.enabledFlag : 1
      })( /*#__PURE__*/React.createElement(Switch, null))), isEdit && itemData.typeCode === 'WEB_HOOK' && _isNumber(itemData.objectVersionNumber) && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
        style: {
          textAlign: 'right',
          marginBottom: '12px'
        }
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.deleteUser"),
          type: 'button',
          meaning: '消息发送配置-删除'
        }],
        style: {
          marginRight: 8
        },
        onClick: this.handleDeleteWebHook,
        disabled: selectedRowKeys.length === 0 || deleteLoading
      }, intl.get('cpaas.common.button.delete').d('删除')), /*#__PURE__*/React.createElement(_Lov, {
        name: "code",
        mode: "button",
        color: "primary",
        placeholder: intl.get('cpaas.common.button.create').d('新建'),
        dataSet: ds,
        clearButton: false,
        modalProps: {
          zIndex: 1000
        },
        className: styles['lov-button']
      })), /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: "tempServerWhId",
        loading: fetchLoading,
        columns: columns,
        pagination: pagination,
        dataSource: webHookData,
        style: {
          marginTop: 14
        },
        scroll: {
          x: tableScrollWidth(columns)
        },
        rowSelection: {
          selectedRowKeys: selectedRowKeys,
          onChange: this.handleRowSelectionChange
        },
        onChange: this.handleTableChange
      }))));
    }
  }]);

  return Drawer;
}(PureComponent), _class3.defaultProps = {
  anchor: 'right',
  title: '',
  visible: false,
  onOk: function onOk(e) {
    return e;
  },
  onCancel: function onCancel(e) {
    return e;
  }
}, _class3), (_applyDecoratedDescriptor(_class2.prototype, "handleTableChange", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "saveBtn", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "saveBtn"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "changeTypeCode", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "changeTypeCode"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "getServerName", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "getServerName"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onChangeTemplate", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "onChangeTemplate"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRowSelectionChange", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRowSelectionChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleClearSelection", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleClearSelection"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDeleteWebHook", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDeleteWebHook"), _class2.prototype)), _class2)) || _class);
export { Drawer as default };